﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestWebApplication.CommerceTransaction;
using TestWebApplication.CommerceTestDataProvider;
using Microsoft.Commerce.Proxy.TransactionService.v201001;
using TestWebApplication.CommercePaymentInstrument;
using CommercePIProxy = Microsoft.Commerce.Proxy.PaymentInstrumentService;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;
using System.Drawing;

namespace TestWebApplication
{
    public partial class ShowPurchases : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<PurchaseInfo> purchases = PurchaseHandler.purchases;
            string accountID = (string)Session["accountID"];
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            if (purchases != null)
            {
                List<GetPurchaseOutput> result = PurchaseHandler.showPurchases(purchases, accountID, requester);
                int i = 0;
                foreach (GetPurchaseOutput purchase in result)
                {
                    PurchaseOutput purchaseOutput = purchase.PurchaseOutputList[0];
                    PurchaseInfoOutput info = purchaseOutput.PurchaseInfoOutput;

                    TableRow row = new TableRow();
                    purchaseTable.Rows.Add(row);

                    TableCell cell = new TableCell();
                    cell.Text = info.PurchaseId;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = info.PurchaseBundleOutputSet[0].Title;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = info.PurchaseBundleOutputSet[0].Currency;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = purchaseOutput.TotalPriceWithoutTax.ToString();
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = purchaseOutput.TotalTax.ToString();
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = info.State.ToString();
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    Button button = new Button();
                    button.CommandArgument = i + "&" + purchaseOutput.TotalPriceWithoutTax;
                    button.BackColor = Color.Gainsboro;
                    button.BorderColor = Color.HotPink;
                    button.BorderStyle = BorderStyle.Solid;
                    button.BorderWidth = 1;
                    button.Text = "Full Refund";
                    button.Width = 80;
                    button.Height = 40;
                    button.Command += new CommandEventHandler(this.refund_Click);
                    cell.Controls.Add(button);
                    row.Cells.Add(cell);

                    row = new TableRow();
                    purchaseTable.Rows.Add(row);
                    cell = new TableCell();
                    cell.ColumnSpan = 7;
                    row.Cells.Add(cell);
                    Table table = new Table();
                    table.GridLines = GridLines.Both;
                    table.HorizontalAlign = HorizontalAlign.Center;
                    table.CellPadding = 15;
                    table.Width = Unit.Percentage(95);
                    cell.Controls.Add(table);

                    foreach (OrderOutput order in info.OrderSet)
                    {
                        row = new TableRow();
                        table.Rows.Add(row);

                        cell = new TableCell();
                        cell.Text = order.OrderId.ToString();
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = order.Currency;
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = order.EstimatedChargeTime.ToString();
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = order.BillingMode.ToString();
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = order.Status;
                        row.Cells.Add(cell);

                        cell = new TableCell();
                        cell.Text = order.ItemEntries[0].UnitAmount.ToString();
                        row.Cells.Add(cell);
                        
                    }

                    i++;
                }
            }
        }

        protected void refund_Click(object sender, CommandEventArgs e)
        {
            string[] args = ((string)e.CommandArgument).Split('&');
            int index = int.Parse(args[0]);
            string amount = args[1];

            string accountID = (string)Session["accountID"];
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];

            PurchaseHandler.refundPurchase(accountID, requester, index, amount);

            Response.Redirect("ShowPurchases.aspx");

        }
    }
}